using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Runtime.InteropServices;

namespace 物理计算小工具
{


    public partial class Form1 : Form
    {
        string path_config = System.IO.Directory.GetCurrentDirectory() + "/config.ini";
        string path_language = System.IO.Directory.GetCurrentDirectory() + "/language";
        [DllImport("kernel32")]
        private static extern int GetPrivateProfileInt(
         string lpAppName,// 指向包含 Section 名称的字符串地址
              string lpKeyName,// 指向包含 Key 名称的字符串地址
              int nDefault,// 如果 Key 值没有找到，则返回缺省的值是多少
              string lpFileName
         ); 
        [DllImport("kernel32")]
        private static extern int GetPrivateProfileString(
          string lpAppName,// 指向包含 Section 名称的字符串地址
　　　　　 string lpKeyName,// 指向包含 Key 名称的字符串地址
　　　　　 string lpDefault,// 如果 Key 值没有找到，则返回缺省的字符串的地址
　　　　　 StringBuilder lpReturnedString,// 返回字符串的缓冲区地址
　　　　　 int nSize,// 缓冲区的长度
　　　　　 string lpFileName
 );

        [DllImport("kernel32")]
        private static extern bool WritePrivateProfileString(
         string lpAppName,// 指向包含 Section 名称的字符串地址
              string lpKeyName,// 指向包含 Key 名称的字符串地址
              string lpString,// 要写的字符串地址
              string lpFileName
         );

        public Form1()
        {
            InitializeComponent();
        }



        private void 汉语ToolStripMenuItem_Click(object sender, EventArgs e)//切换语言为中文
        {
            chineseToolStripMenuItem.Checked = true;
            englishToolStripMenuItem.Checked = false;
            WritePrivateProfileString("set", "language", "chinese", path_config);
            language("chinese");
        }

        private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            System.Environment.Exit(0);//退出程序
        }

        private void 关于ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            about about = new about();
            about.Show();//载入about窗体
        }


        private void Form1_Load(object sender, EventArgs e)
        {
            System.Text.StringBuilder temp = new System.Text.StringBuilder(255);
            GetPrivateProfileString("set", "language", "", temp, 255, path_config);//读配置文件
            string language_ = Convert.ToString(temp);
            if (language_ == "english")//初始化语言
            {
                language("english");
                englishToolStripMenuItem.Checked = true;
                chineseToolStripMenuItem.Checked = false;
            }
        }

        private void menuStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {

        }
        private void language(string language)//切换语言
        {
            string lin;
            System.Text.StringBuilder temp = new System.Text.StringBuilder(255);
            GetPrivateProfileString(language, "菜单", "", temp, 255, path_language + "/" + language + ".ini");
            lin = Convert.ToString(temp);
            菜单ToolStripMenuItem.Text = lin;
            GetPrivateProfileString(language, "帮助", "", temp, 255, path_language + "/" + language + ".ini");
            lin = Convert.ToString(temp);
            帮助ToolStripMenuItem.Text = lin;
            GetPrivateProfileString(language, "语言", "", temp, 255, path_language + "/" + language + ".ini");
            lin = Convert.ToString(temp);
            语言ToolStripMenuItem.Text = lin;
            GetPrivateProfileString(language, "退出", "", temp, 255, path_language + "/" + language + ".ini");
            lin = Convert.ToString(temp);
            退出ToolStripMenuItem.Text = lin;
            GetPrivateProfileString(language, "关于", "", temp, 255, path_language + "/" + language + ".ini");
            lin = Convert.ToString(temp);
            关于ToolStripMenuItem.Text = lin;
            GetPrivateProfileString(language, "中文简体", "", temp, 255, path_language + "/" + language + ".ini");
            lin = Convert.ToString(temp);
            chineseToolStripMenuItem.Text = lin;
            GetPrivateProfileString(language, "English", "", temp, 255, path_language + "/" + language + ".ini");
            lin = Convert.ToString(temp);
            englishToolStripMenuItem.Text = lin;
            GetPrivateProfileString(language, "计算", "", temp, 255, path_language + "/" + language + ".ini");
            lin = Convert.ToString(temp);
            button1.Text = lin; button2.Text = lin; button17.Text = lin; button18.Text = lin;
            GetPrivateProfileString(language, "全部清空", "", temp, 255, path_language + "/" + language + ".ini");
            lin = Convert.ToString(temp);
            button16.Text = lin;
            GetPrivateProfileString(language, "换算", "", temp, 255, path_language + "/" + language + ".ini");
            lin = Convert.ToString(temp);
            button3.Text = lin; button4.Text = lin; button5.Text = lin; button6.Text = lin; button7.Text = lin;
            button8.Text = lin; button9.Text = lin; button10.Text = lin; button11.Text = lin; button12.Text = lin;
            button13.Text = lin; button14.Text = lin; button15.Text = lin;
            GetPrivateProfileString(language, "路程", "", temp, 255, path_language + "/" + language + ".ini");
            lin = Convert.ToString(temp);
            label1.Text = "s" + "(" + lin + ")";
            GetPrivateProfileString(language, "时间", "", temp, 255, path_language + "/" + language + ".ini");
            lin = Convert.ToString(temp);
            label3.Text = "t" + "(" + lin + ")";
            GetPrivateProfileString(language, "速度", "", temp, 255, path_language + "/" + language + ".ini");
            lin = Convert.ToString(temp);
            label5.Text = "ν" + "(" + lin + ")";
            groupBox1.Text = lin;
            GetPrivateProfileString(language, "欧姆定律求电流", "", temp, 255, path_language + "/" + language + ".ini");
            lin = Convert.ToString(temp);
            groupBox3.Text = lin;
            GetPrivateProfileString(language, "重力", "", temp, 255, path_language + "/" + language + ".ini");
            lin = Convert.ToString(temp);
            groupBox4.Text = lin; label41.Text = "G" + "(" + lin + ")";
            GetPrivateProfileString(language, "质量", "", temp, 255, path_language + "/" + language + ".ini");
            lin = Convert.ToString(temp);
            label9.Text = "m" + "(" + lin + ")"; label45.Text = "m" + "(" + lin + ")";
            GetPrivateProfileString(language, "密度", "", temp, 255, path_language + "/" + language + ".ini");
            lin = Convert.ToString(temp);
            label11.Text = "v" + "(" + lin + ")";
            GetPrivateProfileString(language, "密度", "", temp, 255, path_language + "/" + language + ".ini");
            lin = Convert.ToString(temp);
            label13.Text = "ρ" + "(" + lin + ")";
            groupBox2.Text = lin;
            GetPrivateProfileString(language, "力学计算", "", temp, 255, path_language + "/" + language + ".ini");
            lin = Convert.ToString(temp);
            tabPage1.Text = lin;
            GetPrivateProfileString(language, "热学计算", "", temp, 255, path_language + "/" + language + ".ini");
            lin = Convert.ToString(temp);
            tabPage2.Text = lin;
            GetPrivateProfileString(language, "电学计算", "", temp, 255, path_language + "/" + language + ".ini");
            lin = Convert.ToString(temp);
            tabPage3.Text = lin;
            GetPrivateProfileString(language, "单位换算", "", temp, 255, path_language + "/" + language + ".ini");
            lin = Convert.ToString(temp);
            tabPage4.Text = lin;
            GetPrivateProfileString(language, "面积", "", temp, 255, path_language + "/" + language + ".ini");
            lin = Convert.ToString(temp);
            tabPage5.Text = lin;
            GetPrivateProfileString(language, "热量", "", temp, 255, path_language + "/" + language + ".ini");
            lin = Convert.ToString(temp);
            tabPage6.Text = lin;
            GetPrivateProfileString(language, "功率", "", temp, 255, path_language + "/" + language + ".ini");
            lin = Convert.ToString(temp);
            tabPage7.Text = lin;
            GetPrivateProfileString(language, "体积", "", temp, 255, path_language + "/" + language + ".ini");
            lin = Convert.ToString(temp);
            tabPage8.Text = lin;
            GetPrivateProfileString(language, "长度", "", temp, 255, path_language + "/" + language + ".ini");
            lin = Convert.ToString(temp);
            tabPage9.Text = lin;
            GetPrivateProfileString(language, "压力", "", temp, 255, path_language + "/" + language + ".ini");
            lin = Convert.ToString(temp);
            tabPage10.Text = lin;
            GetPrivateProfileString(language, "温度", "", temp, 255, path_language + "/" + language + ".ini");
            lin = Convert.ToString(temp);
            tabPage11.Text = lin;
            GetPrivateProfileString(language, "密度", "", temp, 255, path_language + "/" + language + ".ini");
            lin = Convert.ToString(temp);
            tabPage12.Text = lin;
            GetPrivateProfileString(language, "力", "", temp, 255, path_language + "/" + language + ".ini");
            lin = Convert.ToString(temp);
            tabPage13.Text = lin;
            GetPrivateProfileString(language, "时间", "", temp, 255, path_language + "/" + language + ".ini");
            lin = Convert.ToString(temp);
            tabPage14.Text = lin;
            GetPrivateProfileString(language, "速度", "", temp, 255, path_language + "/" + language + ".ini");
            lin = Convert.ToString(temp);
            tabPage15.Text = lin;
            GetPrivateProfileString(language, "数据存储", "", temp, 255, path_language + "/" + language + ".ini");
            lin = Convert.ToString(temp);
            tabPage16.Text = lin;
            GetPrivateProfileString(language, "角度", "", temp, 255, path_language + "/" + language + ".ini");
            lin = Convert.ToString(temp);
            tabPage17.Text = lin;
            GetPrivateProfileString(language, "电学", "", temp, 255, path_language + "/" + language + ".ini");
            lin = Convert.ToString(temp);
            tabPage18.Text = lin;
            GetPrivateProfileString(language, "物理计算小工具", "", temp, 255, path_language + "/" + language + ".ini");
            lin = Convert.ToString(temp);
            this.Text = lin;

        }


        private void englishToolStripMenuItem_Click(object sender, EventArgs e)//切换语言为English
        {
            englishToolStripMenuItem.Checked = true;
            chineseToolStripMenuItem.Checked = false;
            WritePrivateProfileString("set", "language", "english", path_config);
            language("english");
        }


        private void textBox3_TextChanged(object sender, EventArgs e)
        {

        }
        private double chengfa(double a, double b)
        {
            double c;
            c = a * b;
            return c;
        }
        private double chufa(double a, double b)
        {
            double c;
            c = a / b;
            return c;
        }

        private void button1_Click(object sender, EventArgs e)//速度计算
        {
            try
            {
                double s = Convert.ToDouble(textBox1.Text);//声明变量存储路程
                double t = Convert.ToDouble(textBox2.Text);//声明变量存储时间
                double v = s / t;//声明变量存储速度并计算
                textBox3.Text = Convert.ToString(v);//显示速度

            }
            catch
            {
                error();
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                double m = Convert.ToDouble(textBox4.Text);//声明变量存储质量
                double v = Convert.ToDouble(textBox5.Text);//声明变量存储体积
                double p = m / v;//声明变量存储密度并计算
                textBox6.Text = Convert.ToString(p);//显示密度

            }
            catch
            {
                error();
            }
        }
        private void error()
        {
            System.Text.StringBuilder temp = new System.Text.StringBuilder(255); string language;
            GetPrivateProfileString("set", "language", "", temp, 255, path_config);
            language = Convert.ToString(temp);
            string lin;
            if (language == "chinese")
            {
                GetPrivateProfileString("chinese", "输入错误", "", temp, 255, path_language + "/chinese.ini");
                lin = Convert.ToString(temp);
                MessageBox.Show(lin + "！", "提示");
            }
            if (language == "english")
            {
                GetPrivateProfileString("english", "输入错误", "", temp, 255, path_language + "/english.ini");
                lin = Convert.ToString(temp);
                MessageBox.Show(lin + "！", "Hint");
            }


        }

        private void 菜单ToolStripMenuItem_MouseMove(object sender, MouseEventArgs e)
        {

        }

        private void button16_Click(object sender, EventArgs e)//清空文本框
        {
            textBox7.Text = "";
            textBox8.Text = ""; textBox19.Text = "";
            textBox9.Text = ""; textBox10.Text = ""; textBox11.Text = ""; textBox12.Text = ""; textBox13.Text = "";
            textBox14.Text = ""; textBox15.Text = ""; textBox16.Text = ""; textBox17.Text = ""; textBox18.Text = "";
        }

        private void button3_Click(object sender, EventArgs e)//平方公里的换算
        {
            try
            {
                double km = Convert.ToDouble(textBox7.Text);
                //double ha, shimou, m, dm, cm, mm, sqmi, yingmou, sqrd, sqyd, sqft, sqin;
                textBox8.Text = Convert.ToString(chengfa(km, 100));
                textBox9.Text = Convert.ToString(chengfa(km, 1500));
                textBox10.Text = Convert.ToString(chengfa(km, 1000000));
                textBox11.Text = Convert.ToString(chengfa(km, 100000000));
                textBox12.Text = Convert.ToString(chengfa(km, 10000000000));
                textBox13.Text = Convert.ToString(chengfa(km, 1000000000000));
                textBox14.Text = Convert.ToString(chengfa(km, 0.3861));
                textBox15.Text = Convert.ToString(chengfa(km, 247.1));
                textBox16.Text = Convert.ToString(chengfa(km, 39536.9));
                textBox17.Text = Convert.ToString(chengfa(km, 1195990));
                textBox18.Text = Convert.ToString(chengfa(km, 9000000));
                textBox19.Text = Convert.ToString(chengfa(km, 900000000));
            }
            catch
            {
                error();
            }
        }

        private void button4_Click(object sender, EventArgs e)//公顷的换算
        {
            double ha = Convert.ToDouble(textBox8.Text);
            textBox7.Text = Convert.ToString(chufa(ha, 100));
            textBox9.Text = Convert.ToString(chengfa(ha, 15));
            textBox10.Text = Convert.ToString(chengfa(ha, 10000));
            textBox11.Text = Convert.ToString(chengfa(ha, 1000000));
            textBox12.Text = Convert.ToString(chengfa(ha, 100000000));
            textBox13.Text = Convert.ToString(chengfa(ha, 10000000000));
            textBox14.Text = Convert.ToString(chengfa(ha, 0.003861));
            textBox15.Text = Convert.ToString(chengfa(ha, 2.471));
            textBox16.Text = Convert.ToString(chengfa(ha, 395.369));
            textBox17.Text = Convert.ToString(chengfa(ha, 11959.9));
            textBox18.Text = Convert.ToString(chengfa(ha, 90000));
            textBox19.Text = Convert.ToString(chengfa(ha, 9000000));
        }

        private void button5_Click(object sender, EventArgs e)//市亩的换算
        {
            double shimou = Convert.ToDouble(textBox9.Text);
            textBox7.Text = Convert.ToString(chengfa(shimou, 0.0006667));
            textBox8.Text = Convert.ToString(chengfa(shimou, 0.0666667));
            textBox10.Text = Convert.ToString(chengfa(shimou, 666.66666666));
            textBox11.Text = Convert.ToString(chengfa(shimou, 66666.6666666));
            textBox12.Text = Convert.ToString(chengfa(shimou, 6666666.666666));
            textBox13.Text = Convert.ToString(chengfa(shimou, 666666666.666666));
            textBox14.Text = Convert.ToString(chengfa(shimou, 0.0002573999999999));
            textBox15.Text = Convert.ToString(chengfa(shimou, 0.16473333333333331));
            textBox16.Text = Convert.ToString(chengfa(shimou, 26.357933333333));
            textBox17.Text = Convert.ToString(chengfa(shimou, 797.32666666666));
            textBox18.Text = Convert.ToString(chengfa(shimou, 6000));
            textBox19.Text = Convert.ToString(chengfa(shimou, 600000));
        }

        private void button17_Click(object sender, EventArgs e)//欧姆定律求电流
        {
            try
            {
                double U = Convert.ToDouble(textBox22.Text);
                double R = Convert.ToDouble(textBox21.Text);
                textBox20.Text = Convert.ToString(chufa(U, R));//调用chufa函数计算出电流并显示
            }
            catch
            {
                error();
            }
        }

        private void button18_Click(object sender, EventArgs e)//计算重力
        {
            try
            {
                double m = Convert.ToDouble(textBox25.Text);
                double g = Convert.ToDouble(textBox24.Text);
                textBox23.Text = Convert.ToString(chengfa(m , g));
            }
            catch
            {
                error();
            }
        }

        private void button19_Click(object sender, EventArgs e)//计算液体压强
        {
            try
            {
                double midu = Convert.ToDouble(textBox28.Text);
                double g = Convert.ToDouble(textBox27.Text);
                double h = Convert.ToDouble(textBox29.Text);
                textBox26.Text = Convert.ToString(chengfa(midu , g)*h*1.0);
            }
            catch
            {
                error();
            }
        }

        private void button20_Click(object sender, EventArgs e)//计算功
        {
            try
            {
                double F = Convert.ToDouble(textBox32.Text);
                double s = Convert.ToDouble(textBox31.Text);
                textBox30.Text = Convert.ToString(chengfa(F , s));
            }
            catch
            {
                error();
            }
        }

        private void button21_Click(object sender, EventArgs e)//计算功率
        {
            try
            {
                double W = Convert.ToDouble(textBox35.Text);
                double t = Convert.ToDouble(textBox34.Text);
                textBox33.Text = Convert.ToString(chufa(W, t));
            }
            catch
            {
                error();
            }
        }
    }
}